package com.zgjkhis.modules.outpatient.rest;

import cn.hutool.poi.excel.ExcelFileUtil;
import com.zgjkhis.domain.PageResult;
import com.zgjkhis.exception.ServiceException;
import com.zgjkhis.logging.aop.log.Log;
import com.zgjkhis.modules.member.service.MemberService;
import com.zgjkhis.modules.outpatient.domain.Account;
import com.zgjkhis.modules.outpatient.dto.AccountDto;
import com.zgjkhis.modules.outpatient.dto.AccountQueryCriteria;
import com.zgjkhis.modules.outpatient.service.AccountService;
import com.zgjkhis.modules.security.security.LoginUserUtil;
import com.zgjkhis.modules.util.CharUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @date 2018-11-23
 */
@Api(tags = "门诊：患者信息")
@RestController
@RequestMapping("/account")
public class AccountController {

    private static final String ENTITY_NAME = "account";
    private final AccountService accountService;
    private final MemberService memberService;

    public AccountController(AccountService accountService, MemberService memberService) {
        this.accountService = accountService;
        this.memberService = memberService;
    }

    @Log("查看单个患者")
    @PostMapping(value = "/detail")
    @ApiOperation("查询单个患者")
    //    @PreAuthorize("@el.check('account:datail')")
    public ResponseEntity<Object> getDetail(@RequestBody String id) {
        if (CharUtil.isEmpty(id)) {
            throw new ServiceException("id不能为空");
        }
        return new ResponseEntity<>(accountService.findDetailById(id), HttpStatus.OK);
    }


    @Log("新增患者")
    @ApiOperation("新增患者")
    @PostMapping(value = "/add")
//    @PreAuthorize("@el.check('account:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Account resources) {
        if (resources.getId() != null) {
            throw new ServiceException("A new " + ENTITY_NAME + " cannot already have an ID");
        }
        resources.setPharmacyId(LoginUserUtil.getPharmId());
        resources.setMembershipType("0");
        return new ResponseEntity<>(accountService.save(resources), HttpStatus.OK);
    }


    @Log("修改患者")
    @ApiOperation("修改患者")
    @PostMapping(value = "/update")
//    @PreAuthorize("@el.check('drug:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Account resources) {
        resources.setPharmacyId(LoginUserUtil.getPharmId());
        resources.setMembershipType("0");
        accountService.updateById(resources);
        return ResponseEntity.ok(true);
    }


    @Log("分页查询患者")
    @ApiOperation("分页查询患者")
    @GetMapping(value = "/getList")
//    @PreAuthorize("@el.check('drug:list')")
    public ResponseEntity<PageResult<AccountDto>> getList(AccountQueryCriteria criteria, Pageable pageable) {
        criteria.setPharmacyId(LoginUserUtil.getPharmId());
        return new ResponseEntity<>(accountService.findAllByPageable(criteria, pageable), HttpStatus.OK);
    }

    @Log("批量导入患者")
    @ApiOperation("批量导入患者")
    @PostMapping(value = "/batchsave")
//    @PreAuthorize("@el.check('drug:list')")
    public ResponseEntity<Object> batchsave(@RequestParam("file") MultipartFile file) throws Exception {
        if (file == null) {
            throw new ServiceException("请上传文件");
        }
        if (!ExcelFileUtil.isXls(file.getInputStream()) && !ExcelFileUtil.isXlsx(file.getInputStream())) {
            throw new ServiceException("请上传excel文件");
        }
        accountService.batchSave(file);
        return new ResponseEntity<>(true, HttpStatus.OK);
    }


}
